package tool.careful;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

public class Exporter {

  private static final String BASE_URL = "https://git.oschina.net/zhishi/JavaCN/issues/";
  private static final String OUTPUT_DIR = "backup/";

  /* To avoid hitting service, add some interval between getting each issue */
  private static int INTERVAL_BETWEEN_SAVING_ISSUE = 300;

  public static void main(String[] args) {
    String newDirName = OUTPUT_DIR + DateFormat.getDateTimeInstance().format(new Date()) + "/";
    File file = new File(newDirName);
    // if folder doesn't exist, then create it
    if (!file.exists()) {
      file.mkdirs();
    }

    int issueIndex = 1;
    System.out.println("Backing up to " + newDirName);
    while (saveIssueAsRawHtml(issueIndex++, newDirName)) {
      ;
    }

    System.out.println("Saving all issues finished");
  }

  private static boolean saveIssueAsRawHtml(int issueIndex, String path) {
    try {
      String outputFile = path + "issue" + issueIndex + ".html";

      String indexUrl = BASE_URL + issueIndex;
      InputStream is = getInputStreamByUrl(indexUrl);
      if (is == null) {
        System.out.println("Url not opened: " + indexUrl);
        return false;
      }
      String htmlContent = "";
      for (Element comment : Jsoup.parse(is, "utf-8", indexUrl).select("div.ui.comments")) {
        htmlContent += comment.outerHtml();
      }
      System.out.println("issue #" + issueIndex + " finished\n");

      Thread.sleep(INTERVAL_BETWEEN_SAVING_ISSUE);

      return writeToFile(outputFile, htmlContent);
    } catch (IOException | InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return false;
    }
  }

  private static InputStream getInputStreamByUrl(String urlString) {
    try {
      return new URL(urlString).openConnection().getInputStream();
    } catch (IOException e) {
      return null;
    }
  }

  private static boolean writeToFile(String filename, String content) {
    try {
      File file = new File(filename);

      // if file doesn't, then create it
      if (!file.exists()) {
        file.createNewFile();
      }
      BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
      bw.write(content);
      bw.close();
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }
}
